package com.javachmapi.pay.modules.wxpay.controller;

import com.javachmapi.pay.entity.weixin.PreOrder;
import com.javachmapi.pay.modules.wxpay.service.WxPayService;
import com.javachmapi.pay.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * create by 宋利军
 */
@RestController
@RequestMapping("/wxpay")
@Api(value = "WxPayController", tags = "微信支付接口", description = "扫码支付、H5支付、小程序支付、APP支付")
public class WxPayController {

    @Autowired
    private WxPayService wxPayService;

    /**
     * 方式一、扫码支付
     * @param preOrder
     * @RequestBody PreOrder preOrder
     * @return
     */
    @GetMapping("/createNavite")
    @ApiOperation(value = "Navite支付", notes = "Navite支付-获取支付二维码")
    public String createNavite(){
        System.out.println("进入方法createNavite·····");
        String out_trade_no = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        int total_fee = 1;
        String product_id = "1";
        String body = "这个是一个测试产品，测试Navite支付";
        PreOrder preOrder = new PreOrder();
        preOrder.setOutTradeNo(out_trade_no);
        preOrder.setTotalFee(total_fee);
        preOrder.setProductId(product_id);
        preOrder.setBody(body);
        return wxPayService.createNavite(preOrder);
    }

    /**
     * 方式二、扫码支付
     * @param preOrder
     * @RequestBody PreOrder preOrder
     * @return
     */
    @GetMapping("/createJSAPI")
    @ApiOperation(value = "JSAPI支付", notes = "JSAPI支付-获取支付二维码")
    public String createJSAPI(){
        System.out.println("进入方法createJSAPI·····");
        PreOrder preOrder = new PreOrder();
        preOrder.setOpenid("wx26ae82297d95e1f0");
        //校验
        if(StringUtil.isEmpty(preOrder.getOpenid())){
            return "openid为空！";
        }
        String out_trade_no = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        int total_fee = 1;
        String product_id = "1";
        String body = "这个是一个测试产品,测试JSAPI支付";
        preOrder.setOutTradeNo(out_trade_no);
        preOrder.setTotalFee(total_fee);
        preOrder.setProductId(product_id);
        preOrder.setBody(body);
        return wxPayService.createJSAPI(preOrder);
    }
}
